﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Travianer.Model;
using System.Text.RegularExpressions;
using Travianer.Common;

namespace Travianer.Controller
{
    public sealed class DataController
    {
        private static readonly DataController _Instance;

        static DataController()
        {
            _Instance = new DataController();
        }

        public static DataController GetInstance()
        {
            return _Instance;
        }

        private DataController()
        {

        }

        public IList<Village> GetVillageList(string data)
        {
            IList<Village> lst = new List<Village>();
            Regex reg = new Regex(RegexString.villages);
            MatchCollection mats = reg.Matches(data);
            foreach (Match mat in mats)
            {
                lst.Add(new Village()
                {
                    VID = mat.Groups["id"].Value,
                    VillageName = mat.Groups["name"].Value,
                    IsActive = !string.IsNullOrEmpty(mat.Groups["act"].Value)
                });
            }
            return lst;
        }

        public Village GetVillageInfomation(string data)
        {
            Village ent = new Village();
            Regex reg = new Regex(RegexString.resources);
            MatchCollection mats = reg.Matches(data);
            foreach (Match mat in mats)
            {
                var type = mat.Groups["type"].Value;
                switch (type)
                {
                    case "4":
                        ent.Wood = int.Parse(mat.Groups["res"].Value);
                        ent.Warehourse = int.Parse(mat.Groups["warehouse"].Value);
                        break;
                    case "3":
                        ent.Clay = int.Parse(mat.Groups["res"].Value);
                        break;
                    case "2":
                        ent.Iron = int.Parse(mat.Groups["res"].Value);
                        break;
                    case "1":
                        ent.Crop = int.Parse(mat.Groups["res"].Value);
                        ent.Granary = int.Parse(mat.Groups["warehouse"].Value);
                        break;
                }
            }
            return ent;
        }

        public string GetYZCode(string data)
        {
            string strYZCode;
            Regex reg = new Regex(RegexString.yzCode);
            MatchCollection mats = reg.Matches(data);
            if (mats.Count > 0)
            {
                strYZCode = mats[0].Groups["yzCode"].Value;
            }
            else
            {
                strYZCode = string.Empty;
            }
            return strYZCode;
        }

        public int GetResIDByCategory(string data, string category)
        {
            var entVillage = GetVillageInfomation(data);
            entVillage.village1 = GetVillage1Infomation(data);
            int intResID = -1;
            int intResLevel = 999999;
            switch (category)
            {
                case "普田":
                    //计算等级最低的资源田
                    foreach (var v in entVillage.village1)
                    {
                        if (v.Level < intResLevel)
                        {
                            intResID = v.ID;
                            intResLevel = v.Level;
                        }
                    }
                    break;
                case "智能普田":
                    //计算当前资源最少的名称及数量
                    string strLowestRes = Configuration.Wood;
                    long longLowestResCount = entVillage.Wood;
                    if (entVillage.Clay < longLowestResCount)
                    {
                        strLowestRes = Configuration.Clay;
                        longLowestResCount = entVillage.Clay;
                    }
                    if (entVillage.Iron < longLowestResCount)
                    {
                        strLowestRes = Configuration.Iron;
                        longLowestResCount = entVillage.Iron;
                    }
                    if (entVillage.Crop * 2 < longLowestResCount)
                    {
                        strLowestRes = Configuration.Crop;
                        longLowestResCount = entVillage.Crop;
                    }
                    //计算等级最低的资源田
                    foreach (var v in entVillage.village1)
                    {
                        if (v.Name.IsIn(strLowestRes)
                            && v.Level < intResLevel)
                        {
                            intResID = v.ID;
                            intResLevel = v.Level;
                        }
                    }
                    break;
                case "15T":
                    //计算等级最低的资源田
                    foreach (var v in entVillage.village1)
                    {
                        //排除非粮食资源田
                        if (!v.Name.IsIn(Configuration.Crop))
                            continue;
                        if (v.Level < intResLevel)
                        {
                            intResID = v.ID;
                            intResLevel = v.Level;
                        }
                    }
                    break;
            }
            return intResID;
        }

        public IList<Village1> GetVillage1Infomation(string data)
        {
            IList<Village1> lst = new List<Village1>();
            Regex reg = new Regex(RegexString.village1);
            MatchCollection mats = reg.Matches(data);
            foreach (Match mat in mats)
            {
                lst.Add(new Village1()
                {
                    ID = int.Parse(mat.Groups["id"].Value),
                    Name = mat.Groups["name"].Value,
                    Level = int.Parse(mat.Groups["level"].Value)
                });
            }
            return lst;
        }

        public IList<Village2> GetVillage2Infomation(string data)
        {
            IList<Village2> lst = new List<Village2>();
            Regex reg = new Regex(RegexString.village2);
            MatchCollection mats = reg.Matches(data);
            foreach (Match mat in mats)
            {
                if (lst.Where(e => e.ID == int.Parse(mat.Groups["id"].Value)).Count() > 0)
                    continue;
                var result = GetVillage2NameAndLevelByTitle(mat.Groups["title"].Value);
                lst.Add(new Village2()
                {
                    ID = int.Parse(mat.Groups["id"].Value),
                    Name = result.Item1,
                    Level = result.Item2
                });
            }
            return lst.OrderBy(e => e.ID).ToList();
        }

        private Tuple<string, int> GetVillage2NameAndLevelByTitle(string title)
        {
            //40 Build a Wall
            //40 Wall level 1
            //39 Build a Rally Point
            //39 Rally Point level 1
            //Empty place
            //Granary level 8
            string split = " level ";
            if (title.IndexOf(split) > 0)
                return Tuple.Create(title.Substring(0, title.IndexOf(split)), int.Parse(title.Substring(title.IndexOf(split) + split.Length)));
            else
                return Tuple.Create("Empty", 0);
        }

        public IList<Troop> GetTroopInfomation(string data)
        {
            return new List<Troop>();
        }
    }
}
